package com.waypolice.manager.service.impl;

import com.waypolice.base.BaseServiceImpl;
import com.waypolice.common.annotation.BaseService;
import com.waypolice.common.utils.MathUtils;
import com.waypolice.manager.service.UpmsOrganizationService;
import com.waypolice.mapper.UpmsOrganizationMapper;
import com.waypolice.mapper.UpmsUserOrganizationMapper;
import com.waypolice.pojo.UpmsOrganization;
import com.waypolice.pojo.UpmsOrganizationExample;

import com.waypolice.pojo.UpmsUserOrganization;
import com.waypolice.pojo.UpmsUserOrganizationExample;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* UpmsOrganizationService实现
* Created by shuzheng on 2017/3/20.
 *
*/
@Service
@Transactional
@BaseService
public class UpmsOrganizationServiceImpl extends BaseServiceImpl<UpmsOrganizationMapper, UpmsOrganization, UpmsOrganizationExample> implements UpmsOrganizationService {

    private static final Logger logger = LoggerFactory.getLogger(UpmsOrganizationServiceImpl.class);

    @Autowired
    UpmsOrganizationMapper upmsOrganizationMapper;

    @Autowired
    UpmsUserOrganizationMapper upmsUserOrganizationMapper;

    @Override
    public List<UpmsOrganization> getOrganization(int id) {
        List<UpmsOrganization> arrayList=new ArrayList<>();
        UpmsUserOrganizationExample example=new UpmsUserOrganizationExample();
        UpmsUserOrganizationExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        List<UpmsUserOrganization> upmsUserOrganizations = upmsUserOrganizationMapper.selectByExample(example);
        if(null!=upmsUserOrganizations){
            for(UpmsUserOrganization x:upmsUserOrganizations){
                Integer organizationId = x.getOrganizationId();
                UpmsOrganization upmsOrganization = upmsOrganizationMapper.selectByPrimaryKey(organizationId);
                if (upmsOrganization==null){
                    continue;
                }
                arrayList.add(upmsOrganization);
            }
            return arrayList;
        }
        return null;
    }

    /**
     * @author:zyp
     * @description 根绝部门id获取组织部门
     * @date: 2019/3/20 14:45
     * @return 
     */
    @Override
    public UpmsOrganization getOrganizationById(long borDeptId) {
        return upmsOrganizationMapper.selectByPrimaryKey((int)borDeptId);
    }

    @Override
    public List<UpmsOrganization> findAllOrganization() {
        UpmsOrganizationExample upmsOrganizationExample = new UpmsOrganizationExample();
        UpmsOrganizationExample.Criteria criteria = upmsOrganizationExample.createCriteria();
        List<UpmsOrganization> upmsOrganizations = selectByExample(upmsOrganizationExample);

        return upmsOrganizations;
    }

    /**
     * @author:zyp
     * @description 判断组织名称是否重复
     * @date: 2019/5/27 9:56
     * @return 
     */
    @Override
    public int haveOrganizationName(String name, String id) {
        //如果id为空，则为新增，判断其名称与其他组织机构名称是否有重复
        int num = 0;
        UpmsOrganizationExample upmsOrganizationExample = new UpmsOrganizationExample();
        UpmsOrganizationExample.Criteria criteria = upmsOrganizationExample.createCriteria();
        if (StringUtils.isBlank(id)){
            criteria.andNameEqualTo(name);
        }else{
            criteria.andNameEqualTo(name);
            criteria.andOrganizationIdNotEqualTo(MathUtils.string2int(id));
        }
        num = upmsOrganizationMapper.countByExample(upmsOrganizationExample);
        return num;
    }
}